package com.atguigu.tingshu.search.mq;

import com.atguigu.tingshu.common.constant.KafkaConstant;
import com.atguigu.tingshu.search.service.impl.SearchServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Component
@Slf4j
public class SearchReceiver {


    private final SearchServiceImpl searchService;

    @Autowired
    public SearchReceiver(SearchServiceImpl searchService) {
        this.searchService = searchService;
    }
    /**
     * @description: kafka监听器 上架
     * @author Petrichor
     * @date 2023/12/27 17:06
     * @param record value()  albumId 专辑id
    **/
    @KafkaListener(topics = KafkaConstant.QUEUE_ALBUM_UPPER)
    public void upperAlbum(ConsumerRecord<String , String> record) {
        // 获取到新注册的用户的id
        log.error("kafka监听器 上架=======================================================");
        Long albumId = Long.parseLong(record.value());
        if(StringUtils.isEmpty(albumId)) {
            return ;
        }
        searchService.upperAlbum(albumId); ;
    }
    /**
     * @description: kafka监听器 下架
     * @author Petrichor
     * @date 2023/12/27 17:10
     * @param record value()  albumId 专辑id
    **/
    @KafkaListener(topics = KafkaConstant.QUEUE_ALBUM_LOWER)
    public void lowerAlbum(ConsumerRecord<String , String> record) {
        // 获取到新注册的用户的id
        log.error("kafka监听器 下架=======================================================");
        Long albumId = Long.parseLong(record.value());
        if(StringUtils.isEmpty(albumId)) {
            return ;
        }
        searchService.lowerAlbum(albumId);
    }
}
